package com.qf.smartdevice.service.Impl;

import com.qf.smartdevice.mapper.MenuMapper;
import com.qf.smartdevice.mapper.RoleMapper;
import com.qf.smartdevice.mapper.UserMapper;
import com.qf.smartdevice.pojo.Menu;
import com.qf.smartdevice.pojo.Role;
import com.qf.smartdevice.pojo.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class MyUserDetailService implements UserDetailsService {

    private UserMapper userMapper;

    private RoleMapper roleMapper;

    private MenuMapper menuMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Autowired
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    @Autowired
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

//      先按照用户名查询用户相关的信息
        User user = userMapper.findUsersByName(username);
        if (user == null) {
            return null;
        }

        List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
//      根据用户的ID 查询除所有的角色
        List<Role> roleList = roleMapper.findAllRolesByUserId(user.getId());
//        当前这个用户里面有多个角色 查出后都是自己的 存里面
        user.setRoleList(roleList);

        Set<Menu> menuSet = new HashSet<>();
//        再根据用户的角色查询除所有的权限
        roleList.forEach(role -> {
//            当前角色能访问的所有权限
            List<Menu> menuList = menuMapper.findMenusByRoleId(role.getId());
//            存在这个角色里面
            role.setMenuList(menuList);

//            把每个角色存在里面
            grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_" + role.getName()));
            List<SimpleGrantedAuthority> authorityList = menuList.stream().map(Menu::getPerms).map(SimpleGrantedAuthority::new).collect(Collectors.toList());
            grantedAuthorityList.addAll(authorityList);
            //我们需要在页面上面展示用户可见的页面列表,所以需要将页面列表单独拿出来,也就是 type 类型为 1 的 menu 对象
            List<Menu> pageList = menuList.stream().filter(menu -> menu.getType() == 1).collect(Collectors.toList());
//            不同角色 可能会有相同的 访问权限  去重一下
            menuSet.addAll(pageList);//添加到用户可以访问的列表中
        });
        //遍历放到新的权限集合
        //spring 提供的一个工具类,让我们可以在任意类中获取到当前的请求对象
        ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest().getSession().setAttribute("pagelist", menuSet);
        return new org.springframework.security.core.userdetails.User(username, user.getPassword(), grantedAuthorityList);


    }
}
